package com.example.prettyprint;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class CustomParser extends DefaultHandler {

	private boolean mInMultiSurface;
	private boolean mInMultiSurfaceMember;
	private int lineCounter = 0;
	@Override
	public void startDocument() throws SAXException {
		++lineCounter;
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes attrs) throws SAXException {
		
		++lineCounter;
		
		if(qName.equalsIgnoreCase("gml:multisurface"))
		{
			this.mInMultiSurface = true;
		}
		else if(this.mInMultiSurface && qName.equalsIgnoreCase("gml:surfaceMember"))
		{
			this.mInMultiSurfaceMember = true;
		}
		else if(this.mInMultiSurfaceMember)
		{
			if(!qName.equalsIgnoreCase("gml:polygon"))
			{
				System.out.printf("%s found at line %d", qName, lineCounter);
				System.out.println();
			}
			
			this.mInMultiSurfaceMember = false;
		}
	}

	public void endElement(String uri, String localName, String qName) {
		++lineCounter;

		if(this.mInMultiSurface && qName.equalsIgnoreCase("gml:surfaceMember"))
		{
			this.mInMultiSurfaceMember = false;
		}
		else if(qName.equalsIgnoreCase("gml:multisurface"))
		{
			this.mInMultiSurface = false;
		}
	}

	public void characters(char buf[], int offset, int len) throws SAXException {
		++lineCounter;
	}
}
